package com.yzf.gameeditor.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class Rich3Manager {

    @Resource
    private MemoryManager memoryManager;

    private final String PROCESS_NAME = "dosbox.exe";

    private final int BASE_ADDRESS = 0x0074b6b0;

    private final int MONEY_ADDRESS = 0x9fb6e;

    private final int POINT_ADDRESS = 0x9fb8e;

    public boolean money(String money) {
        return edit(money, MONEY_ADDRESS, 1);
    }

    public boolean point(String point) {
        return edit(point, POINT_ADDRESS, 1);
    }

    public boolean edit(String value, int address, int loopTotal) {
        boolean flag = false;
        try {
            int process = memoryManager.OpenProcess(PROCESS_NAME);
            if(process != -1) {
                int finalAdd = address;
                for(int i=0; i<loopTotal; i++) {
                    int tempAdd = memoryManager.ReadIntProcessMemory(process, BASE_ADDRESS);
                    finalAdd += tempAdd;
                }
                memoryManager.WriteIntProcessMemory(process, Long.parseLong(value), finalAdd);
                flag = true;
                //退出进程
                //memoryManager.CloseHandle(process);
            }
        } catch (Exception e) {
            log.error("Rich3 edit error: {}", e.getMessage());
        }
        return flag;
    }

}
